<meta charset="utf-8"/>
<?php 
error_reporting();
require_once "../model/spider.class.php";
require_once "../model/dealString.class.php";
require_once "../model/connectdb.php";
$db=new db();
$db->connectdb();
$targetList=$db->select("SELECT * FROM targetList WHERE type=0");
$db->close();
foreach ($targetList as $rowK) {
	$id=$rowK['id'];//当前所执行抓取任务的关键词id
	$spider=new cwantSpider();
	$cat=$rowK['catName'];
	$keywordArr=json_decode($rowK['keyword'],1);
	$urlArr=json_decode($rowK['urlList'],1);
	$result=$spider->getRelativeUrl($keywordArr,$urlArr);//获取到所有相关的链接
	$deal=new dealString();
	$relativeUrl=$deal->encode_json($result);
	//$relativeUrl=json_encode($result);//相关文章地址的json格式
	$db=new db();
	$db->connectdb();
	for($insertNum=0;$insertNum<5;$insertNum++){
		$res=$db->query("INSERT INTO relativeUrl (cat,relativeUrl) VALUES ('$cat','$relativeUrl')");
		if(!$res){
			//echo mysql_error();
			echo "insert url fail";
		}
		else{
			break;
		}
	}
	$db->close();//存储相关地址
	$spider->clear();
	$db=new db();
	$db->connectdb();
	$db->query("UPDATE targetList SET type=1 WHERE id='$id'");
	$db->close();
	
}

//var_dump($spider->crawlContent("http://china.huanqiu.com/article/2015-10/7874535.html"));
// foreach ($content as $row) {
// 	foreach ($row as $key => $value) {
// 		echo $key.":".$value."<br/>";
// 	}
// 	echo "<br/><br/>";
// }	
?>